package net.ikenway.springboot.demo.Utils;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author MasterKenway <zk@ikenway.net>
 * @Description
 * @date Created Date 12/11/2020
 */
@Component
public class MapperUtil {
    private final ModelMapper mapper;

    @Autowired
    public MapperUtil(ModelMapper mapper) {
        this.mapper = mapper;
    }

    public <T, TT> TT map(T source, Class<TT> destinationClass) {
        return this.mapper.map(source, destinationClass);
    }

    public  <T, TT> List<TT> mapList(List<T> source, Class<TT> targetClass) {
        return source.stream().map(item -> this.mapper.map(item, targetClass)).collect(Collectors.toList());
    }
}
